package com.ruoyi.project.notice.category.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.notice.category.domain.Category;
import com.ruoyi.project.notice.category.service.ICategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
// 同求URL前缀
@RequestMapping("/notice/category")
public class CategoryController extends BaseController {
    // 所有页面路径
    private String prefix= "notice/category";
    @Autowired
    private ICategoryService categoryService;

    // Shiro注解，表示访问这个控制器需要什么权限
    @RequiresPermissions("notice:category:view")
    // 没有参数表示访问地址为类上的@RequestMapping
    @GetMapping()
    public String showCategory(){
        // 字符串以/开头
        return prefix +"/view";
    }

    /**
     * 分页显示，外加搜索
     * @param category
     * @return
     */
    @RequiresPermissions("notice:category:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Category category)
    {
        startPage();
        List<Category> list = categoryService.selectCategoryList(category);
        return getDataTable(list);
    }

    /**
     * 新增角色
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存角色
     */
    @RequiresPermissions("notice:category:add")
    @Log(title = "公告分类管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Category category)
    {
        try {
            int index = categoryService.insertCategory(category);
            if(index==1){
                return toAjax(index);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        return error("服务器端消息 - 公告分类管理新增失败");
    }


    /**
     * 单条删除和批量删除
     * @param ids
     * @return
     */
    @RequiresPermissions("notice:category:remove")
    @Log(title = "公告分类管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try {
            Long[] arr = Convert.toLongArray(ids);
            int index = 0 ;
            if(arr.length==1){// 单条
                index= categoryService.deleteById(arr[0]);
            }else{// 多条
                index= categoryService.deleteByIds(arr);
            }
            if(index==1){// 成功
                return toAjax(index);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        return error("服务器端消息 - 公告分类管理删除失败");
    }

    @Log(title = "公告分类管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("notice:category:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Category category)
    {
        // 查询全部数据
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        return util.exportExcel(list, "公告分类数据");
    }

    /**
     * 显示修改页面
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Long id, ModelMap mmap)
    {
        // 把查询到的分类内容放入到request作用域中
        mmap.put("category", categoryService.selectById(id));
        return prefix + "/edit";
    }


    /**
     * 修改保存角色
     */
    @RequiresPermissions("notice:category:edit")
    @Log(title = "公告类别管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Category category)
    {
        try {
            int index = categoryService.updateCategory(category);
            if(index==1){
                return toAjax(index);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  error(e.getMessage());
        }
        return  error("服务器端消息 - 公告分类管理修改失败");
    }
}
